package com.example.file.utils;

/**
 * 文件名称工具类
 */
public class FileNameUtils {

    /**
     * 获取文件的前缀
     * @param name
     * @return
     */
    public static String getPrefixName(String name) {
        if (name == null || name.trim().length() == 0) {
            return "";
        }
        String[] split = name.split("\\.");
        if(split.length==1){
            return split[0];
        }
        if(split.length>=2){
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < split.length-1; i++) {
                sb.append(split[i]).append(".");
            }
            return sb.substring(0,sb.length()-1);
        }
        return "";
    }
    /**
     * 获取文件的前缀
     * @param name
     * @return
     */
    public static String getSuffixName(String name) {
        if (name == null || name.trim().length() == 0) {
            return "";
        }
        String[] split = name.split("\\.");
        if(split.length==1){
            return "";
        }
        if(split.length>=2){
            return split[split.length-1];
        }
        return "";
    }



    public static void main(String[] args) {
        String[] s = {"",null,"aa","my.txt","a.b.c"};

        for (int i = 0; i < s.length; i++) {
            System.out.println("i = " + s[i]);
            System.out.println("前缀: " + getPrefixName(s[i]));
            System.out.println("后缀: " + getSuffixName(s[i]));
        }
    }
}
